
//登录表单提交
function btclick(){
	var username = $('#username').val();
	var password = $('#password').val();
	
	//校验
	if(username==""){
		alert("用户名不能为空！");
		return;
	}else if(password==""){
		alert("密码不能为空!");
		return;
	}
	
	$.ajax({
		url: 'user/loginAction',
		type: 'POST',
		data: {"username": username,"password":password},
	})
	.done(function(data) {
		//var obj = JSON.parse(data);  //js转换服务器返回回来的json数据
		console.log("success");
		var str = data;
		if(str==200){
			window.location.href=getContextPath()+"/user/goMain";
		}else{
			$('#errorSpace').html("登录失败");
		}
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});
	
}


//注册表单提交
function registerClick(){
	var username = $('#username').val();
	var password = $('#password').val();
	var password2 = $('#password2').val();
	var Email =$('#Email').val();

	alert(username+password+password2+Email)
	if(username==""){
		alert('用户名为空');
		return;
	}
	
	if(Email==""){
		alert('Email未填写');
		return;
	}
	
	if(password==""){
		alert('密码为空');
		return;
	}
	if(password2==""){
		alert('密码为空');
		return;
	}
	
	if(password!=password2){
		alert("两次密码不一致");
		return;
	}


	$.ajax({
		url: getContextPath()+'/user/registerAction',
		type: 'POST',
		data: {"username": username,"password":password2,"email":Email},
	})
	.done(function(data) {
		if(data!=500){//注册成功
			alert('恭喜注册成功');
			window.location.href='login.jsp'
		}else{
			alert('注册失败，请重试')
		}
		console.log("success");
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});  //Ajax结束
	
	

}



/**
 * testJSon
 */

function testJSON(){
	var json ={"name":"lisi"}; //这是一个json对象
	var str = '{"name":"dalao"}';//这是一个字符串
	//var obj =eval('('+str+')');//eval()方式转换str>>>json
	//var obj = $.parseJSON(str); //$.parseJSON()方式转换str>>json
	  var obj = JSON.parse(str);//JSON.parse()方式str>>json
	  
	  alert(JSON.stringify(json)); //json对象>>>str
	
	alert(obj.name);
}


function getContextPath() {  //获取根路径

    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    var result = pathName.substr(0,index+1);
    return result;
}



